﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp.Study.AsyncTest
{
    public class TaskThreadDemo
    {
        private readonly static CancellationTokenSource cts = new CancellationTokenSource();
        public static void Go()
        {
            Task task1 = Task.Run(() => TaskFor_Demo());
            Task task2 = Task.Run(() => ThreadFor_Demo());
            Task.WaitAll(task1, task2);
        }

        #region Demo1

        public static void TaskFor_Demo()
        {
            Stopwatch sw = Stopwatch.StartNew();
            Parallel.For(0, 5, new ParallelOptions { MaxDegreeOfParallelism = 20 }, (i) => Task.Delay(2000).Wait());
            sw.Stop();
            Console.WriteLine($"Task 执行时间：{sw.ElapsedMilliseconds} ms");
        }

        public static void ThreadFor_Demo()
        {
            Stopwatch sw = Stopwatch.StartNew();
            Parallel.For(0, 5, new ParallelOptions { MaxDegreeOfParallelism = 20 }, (i) => Thread.Sleep(2000));
            sw.Stop();
            Console.WriteLine($"Thread 执行时间：{sw.ElapsedMilliseconds} ms");
        }
        #endregion


    }
}
